package com.easy.work.db;

import android.content.Context;

import com.easy.work.app.AppSession;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.DeleteBuilder;
import com.j256.ormlite.stmt.QueryBuilder;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


class MessageDao {

    private Dao<MessageBean, Integer> messageDao;

    MessageDao(Context context) {
        DBHelper dbHelper = DBHelper.getHelper(context);
        try {
            messageDao = dbHelper.getDao(MessageBean.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 清空数据库数据
     */
    void clear() {
        try {
            DeleteBuilder<MessageBean, Integer> delete = messageDao.deleteBuilder();
            delete.reset();
            delete.delete();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 添加一条记录
     */
    void add(MessageBean msg) {
        try {
            messageDao.create(msg);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 添加一多条记录
     */
    void add(List<MessageBean> list) {
        try {
            messageDao.create(list);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 更新一条记录
     */
    void update(MessageBean msg) {
        try {
            messageDao.update(msg);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除一条记录
     */
    void delete(MessageBean msg) {
        try {
            messageDao.delete(msg);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除一条记录
     */
    void deleteById(int id) {
        try {
            messageDao.deleteById(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除该userId对应的数据
     */
    void deleteByUserId() {
        try {
            List<MessageBean> msgList = queryAllByUserId();
            if (!msgList.isEmpty()) {
                messageDao.delete(msgList);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除多条记录
     */
    void delete(List<MessageBean> msgList) {
        try {
            messageDao.delete(msgList);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    /**
     * 查询所有记录
     */
    List<MessageBean> queryForAllUser() {
        List<MessageBean> msgList = new ArrayList<>();
        try {
            msgList = messageDao.queryForAll();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return msgList;
    }

    /**
     * 根据userId查询记录
     */
    List<MessageBean> queryAllByUserId() {
        List<MessageBean> msgList = new ArrayList<>();
        try {
            msgList = messageDao.queryForEq("userId", AppSession.getInstance().getUserId());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return msgList;
    }

    /**
     * 根据userId和消息类型查询记录
     */
    List<MessageBean> queryByUserIdAndMsgType(Integer msgType) {
        List<MessageBean> msgList = new ArrayList<>();
        HashMap<String, Object> params = new HashMap<>();
        params.put("userId", AppSession.getInstance().getUserId());
        params.put("msgType", msgType);
        try {
            msgList = messageDao.queryForFieldValues(params);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return msgList;
    }

    /**
     * 根据userId查询面试消息和录用消息
     */
    List<MessageBean> queryByUserIdAndMxLy() {
        String userId = AppSession.getInstance().getUserId();
        List<MessageBean> msgList = new ArrayList<>();
        QueryBuilder<MessageBean, Integer> queryBuilder = messageDao.queryBuilder();
        try {
            queryBuilder.where().eq("msgType", 1).or().eq("msgType", 2).and().eq("userId", userId);
            msgList = queryBuilder.query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return msgList;
    }

    /**
     * 获取系统消息
     *
     * @param isRead 传false为未读，传true为已读
     */
    List<MessageBean> getSystemMsg(boolean isRead) {
        List<MessageBean> msgList = new ArrayList<>();
        HashMap<String, Object> params = new HashMap<>();
        params.put("userId", AppSession.getInstance().getUserId());
        params.put("msgType", 3);
        params.put("readState", isRead);
        try {
            msgList = messageDao.queryForFieldValues(params);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return msgList;
    }

    /**
     * 获取所有未读消息
     */
    List<MessageBean> getUnreadMsgList() {
        List<MessageBean> msgList = new ArrayList<>();
        HashMap<String, Object> params = new HashMap<>();
        params.put("userId", AppSession.getInstance().getUserId());
        params.put("readState", false);
        try {
            msgList = messageDao.queryForFieldValues(params);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return msgList;
    }
}
